
import {Component, OnInit, EventEmitter, Input, Output, Injector} from '@angular/core';
import {EmployeeServiceSvr} from '../../../services/employee.service';
import {CoreComponent} from 'pengesoft-ng-lib';

class MessageService {
}
@Component({
  selector: 'app-enter-files',
  templateUrl: './enter-files.component.html',
  styleUrls: ['./enter-files.component.less']
})
export class EnterFilesComponent extends CoreComponent implements OnInit {

  // 最新时间
  newDate: any = new Date();

  // 所有展示的最新信息
  newMsg: any;

  // 是否为最新消息
  // 培训管理
  isHot1: boolean
  // 薪资
  isHot2: boolean
  // 奖惩
  isHot3: boolean

  // 员工
  EmpId: string;

  // 返回
  @Output() cancelFileShowModal = new EventEmitter();
  @Output() cancelDetailFileShowModal: EventEmitter<any> = new EventEmitter<any>();


  name = '';
  // 接受传过来的员工信息值
  // tslint:disable-next-line:ban-types max-line-length
  acceptEmployee: { EmergencyContact: string; ExtAttr: string; Sex: number; StateId: string; JobName: string; CardAddress: string; CardNum: string; EmployeeDay: number; Birth: Date | string; Politic: string; Name: string; EmergencyPeople: string; CardType: string; Phone: string; DeptName: string; ArchivesFile: string; Age: number; JobId: string; StudyMajor: string; NativePlace: string; IDCard: string; IDPlace: string; NowPlace: string; EducationLevel: string; ArchivesNum: string; Id: string; GraduatedSchool: string; GraduatedTime: Date | string; HouseholdType: string };

  constructor(
    private injector: Injector,
    private employSvr: EmployeeServiceSvr
  ) {
    super(injector);
  }

  ngOnInit(): void {
    this.name = window.sessionStorage.getItem('sendEnterFilesName');
    // this.acceptEmployee = JSON.parse(window.sessionStorage.getItem('sendEnterEmployee'));
    console.log('this.acceptEmployee====', this.name, this.acceptEmployee);
    this.employSvr.getArchivesByEmpId(window.sessionStorage.getItem('sendEnterFilesID')).then(res => {
      this.acceptEmployee = {
        DeptName: res.EmployeeDto.DeptName,
        Age: res.EmployeeDto.Employee.Age,
        ArchivesFile: res.EmployeeDto.Employee.ArchivesFile,
        ArchivesNum: res.EmployeeDto.Employee.ArchivesNum,
        Birth: res.EmployeeDto.Employee.Birth,
        CardAddress: res.EmployeeDto.Employee.CardAddress,
        CardNum: res.EmployeeDto.Employee.CardNum,
        CardType: res.EmployeeDto.Employee.CardType,
        EducationLevel: res.EmployeeDto.Employee.EducationLevel,
        EmergencyContact: res.EmployeeDto.Employee.EmergencyContact,
        EmergencyPeople: res.EmployeeDto.Employee.EmergencyPeople,
        EmployeeDay: res.EmployeeDto.Employee.EmployeeDay,
        ExtAttr: res.EmployeeDto.Employee.ExtAttr,
        GraduatedSchool: res.EmployeeDto.Employee.GraduatedSchool,
        GraduatedTime: res.EmployeeDto.Employee.GraduatedTime,
        HouseholdType: res.EmployeeDto.Employee.HouseholdType,
        IDCard: res.EmployeeDto.Employee.IDCard,
        IDPlace: res.EmployeeDto.Employee.IDPlace,
        Id: res.EmployeeDto.Employee.Id,
        JobId: res.EmployeeDto.Employee.JobId,
        Name: res.EmployeeDto.Employee.Name,
        NativePlace: res.EmployeeDto.Employee.NativePlace,
        NowPlace: res.EmployeeDto.Employee.NowPlace,
        Phone: res.EmployeeDto.Employee.Phone,
        Politic: res.EmployeeDto.Employee.Politic,
        Sex: res.EmployeeDto.Employee.Sex,
        StateId: res.EmployeeDto.Employee.StateId,
        StudyMajor: res.EmployeeDto.Employee.StudyMajor,
        JobName: res.EmployeeDto.JobName
      };
    });
    // 初始化
    this.getAll();
  }

  // 反回上一级
  back(): void {
    this.cancelFileShowModal.emit();
  }

  // 进入详情
  detailFiles(index): void {
    this.cancelDetailFileShowModal.emit(index);
    // @ts-ignore
    window.sessionStorage.setItem('index', index, window.sessionStorage.getItem('sendEnterFilesID'));
    this.employSvr.getArchivesByEmpId(window.sessionStorage.getItem('sendEnterFilesID')).then(res => {
    });
  }

  // 获取所有最新消息展示
  getAll(): void {
    this.EmpId = window.sessionStorage.getItem('sendEnterFilesID')

    this.employSvr.getArchivesByEmpId(this.EmpId).then(res => {
      this.newMsg = res

      const newTrain = this.newMsg.TrainDtos[0].Train.TrainStartTime.substring(0,10);
      const newSalary = this.newMsg.SalaryDto.SalaryRecords[0].ChangeTime.substring(0,10);
      const newRewards = this.newMsg.Rewards[0].RewardTime.substring(0,10);
      this.isHot1 = this.SameWeek(newTrain)
      this.isHot2 = this.SameWeek(newSalary)
      this.isHot3 = this.SameWeek(newRewards)

    })
  }
  /*
    * 判断是否在同一个星期里
    * date:时间字符串
    * return:true/false
  */
  SameWeek(date) {    
    const date1 = new Date(date.replace(/-/g, "/"));  //将传入的时间字符串转换成时间对象    
    const date2 = new Date();     //当前时间   
    const curWeek = date2.getDay();           //获取当前星期几    
    const monday = this.GetDate((curWeek), 1); //计算出星期一    
    const sunday = this.GetDate((7 - curWeek), 2); //计算出星期天
    if (date1.getTime() < monday.getTime() || date1.getTime() > sunday.getTime()) {
        return false;       //不在同一个星期内
      } else {
        return true;        //在同一个星期内
      }
    }

  /*
    *获取当前日期前N天或后N日期(N = day)
    *type:1：前；2：后  
  */
  GetDate(day, type) {    
    var zdate = new Date();    
    var edate;    
    if (type == 1) {        
      edate = new Date(zdate.getTime() - (day * 24 * 60 * 60 * 1000));    
    } else {        
      edate = new Date(zdate.getTime() + (day * 24 * 60 * 60 * 1000));   
    }    
    return edate;
    }
  }
